package com.ruoyi.system.service.estate.impl;

import java.util.Date;
import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.estate.FeeBillMapper;
import com.ruoyi.system.domain.estate.FeeBill;
import com.ruoyi.system.service.estate.IFeeBillService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 收费账单Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class FeeBillServiceImpl implements IFeeBillService 
{
    @Autowired
    private FeeBillMapper feeBillMapper;

    /**
     * 查询收费账单信息
     * 
     * @param billId 收费账单ID
     * @return 收费账单信息
     */
    @Override
    public FeeBill selectFeeBillById(Long billId)
    {
        return feeBillMapper.selectFeeBillById(billId);
    }

    /**
     * 查询收费账单列表
     * 
     * @param feeBill 收费账单信息
     * @return 收费账单
     */
    @Override
    public List<FeeBill> selectFeeBillList(FeeBill feeBill)
    {
        return feeBillMapper.selectFeeBillList(feeBill);
    }

    /**
     * 新增收费账单
     * 
     * @param feeBill 收费账单信息
     * @return 结果
     */
    @Override
    public int insertFeeBill(FeeBill feeBill)
    {
        // 生成账单编号：FB + 年月日时分秒 + 3位随机数
        String billCode = "FB" + DateUtils.dateTimeNow() + String.format("%03d", (int)(Math.random() * 1000));
        feeBill.setBillCode(billCode);
        
        // 设置初始状态为未缴费
        feeBill.setPayStatus("0");

        // 设置缴费截止日期为账单结束日期后30天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(feeBill.getEndDate());
        calendar.add(Calendar.DAY_OF_MONTH, 30);
        feeBill.setDueDate(calendar.getTime());
        
        return feeBillMapper.insertFeeBill(feeBill);
    }

    /**
     * 修改收费账单
     * 
     * @param feeBill 收费账单信息
     * @return 结果
     */
    @Override
    public int updateFeeBill(FeeBill feeBill)
    {
        return feeBillMapper.updateFeeBill(feeBill);
    }

    /**
     * 批量删除收费账单
     * 
     * @param billIds 需要删除的收费账单ID
     * @return 结果
     */
    @Override
    public int deleteFeeBillByIds(Long[] billIds)
    {
        return feeBillMapper.deleteFeeBillByIds(billIds);
    }

    /**
     * 删除收费账单信息
     * 
     * @param billId 收费账单ID
     * @return 结果
     */
    @Override
    public int deleteFeeBillById(Long billId)
    {
        return feeBillMapper.deleteFeeBillById(billId);
    }
    
    /**
     * 处理缴费
     * 
     * @param feeBill 收费账单信息
     * @return 结果
     */
    @Override
    public int payFeeBill(FeeBill feeBill)
    {
        // 获取当前账单信息
        FeeBill bill = feeBillMapper.selectFeeBillById(feeBill.getBillId());
        if (bill == null)
        {
            return 0;
        }
        
        // 设置缴费时间
        feeBill.setPayTime(new Date());
        
        // 判断缴费状态
        if (feeBill.getPaidAmount().compareTo(bill.getTotalAmount()) >= 0)
        {
            // 全额缴费
            feeBill.setPayStatus("1");
        }
        else if (feeBill.getPaidAmount().compareTo(bill.getPaidAmount()) > 0)
        {
            // 部分缴费
            feeBill.setPayStatus("2");
        }
        
        return feeBillMapper.updateFeeBill(feeBill);
    }
} 